package lcr;

import java.util.ArrayList;
import java.util.List;

public class Test0085 {
}

class Solution0085_1 {

    List<String> ans=new ArrayList<>();

    public List<String> generateParenthesis(int n) {
        char[] chars = new char[n * 2];
        traceback(chars,0,n,0);

        return ans;
    }

    public void traceback(char[] chars,int index,int l,int r){
        if (index==chars.length-1){
            ans.add(new String(chars));
            return;
        }

        if (l>0){
            chars[index]='(';
            traceback(chars,index+1,l-1,r+1);
        }

        if (r>0){
            chars[index]=')';
            traceback(chars,index+1,l,r-1);
        }
    }
}
